package com.example.bijidemo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.bijidemo.common.R;
import com.example.bijidemo.common.SysLog;
import com.example.bijidemo.entity.Category;
import com.example.bijidemo.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 新增分类
     * @param category
     * @return
     */
    @PostMapping
    @SysLog("新增项目的操作")
    public R<String> save(@RequestBody Category category){
        log.info("category:{}",category);
        categoryService.save(category);
        return R.success("新增分类成功");
    }
    /*
    分类中的分页主页查询
     */

    @GetMapping("/page")
    @SysLog("分页查询项目的操作")
    public R<Page> page(int page,int pageSize){
//        构造分页构造器
        Page<Category> pageInfo =new Page<>(page,pageSize);
//        构造条件构造器
        LambdaQueryWrapper<Category> lmp=new LambdaQueryWrapper<>();
//        排序过滤
        lmp.orderByDesc(Category::getUpdateTime);
        categoryService.page(pageInfo,lmp);
        return R.success(pageInfo);

    }

    /*
    删除分类
     */
    @DeleteMapping
    @SysLog("删除项目的操作")
    public R<String> delete(@RequestParam("ids") Long id){
        log.info("删除分类，id为：{}",id);
        categoryService.remove(id);
        return R.success("分类信息删除成功");
    }


    /*
    根据id修改
     */
    @PutMapping
    @SysLog("根据id修改项目的操作")
    public R<String> update(@RequestBody Category category){
        log.info("修改分类的信息{}",category);
        categoryService.updateById(category);
        return R.success("修改分类成功");
    }

    /**
     * 根据在新增任务哪一栏找到选择任务分类
     * @param category
     * @return
     */
    @GetMapping("/list")
    public R<List<Category>> list(Category category){
        LambdaQueryWrapper<Category> a=new LambdaQueryWrapper<>();
        a.eq(category.getType()!=null,Category::getType,category.getType());
        a.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);
        List<Category> list = categoryService.list(a);
        return R.success(list);
    }
    @GetMapping("/many")
    public R<List<Category>> getmany(){
        LambdaQueryWrapper<Category> a=new LambdaQueryWrapper<>();
        a.select();
        a.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);
        List<Category> list = categoryService.list(a);
        return R.success(list);


    }


}
